<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="common/include :: header"></head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="role-form">
                <div class="select-list">
                    <ul>
                        <li>
                            用户名称：<input type="text" name="roleName"/>
                        </li>
                        <li>
                            用户性别：<input type="text" name="roleKey"/>
                        </li>
                        <li>
                            用户状态：<select name="status" >
                            <option value="">所有</option>
                            <option ></option>
                        </select>
                        </li>
                        <li class="select-time">
                            <label>创建时间： </label>
                            <input type="text" class="time-input form-control layer-date" id="startTime" placeholder="开始时间" name="params[beginTime]"/>
                            <span>-</span>
                            <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endTime]"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick=""><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick=""><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="btn-group-sm hidden-xs" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="user:add">
                <i class="fa fa-plus"></i> 新增
            </a>
            <a class="btn btn-primary btn-edit disabled" onclick="$.operate.edit()" shiro:hasPermission="user:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <!--<a class="btn btn-danger btn-del disabled" onclick="$.operate.removeAll()" shiro:hasPermission="user:delete">
                <i class="fa fa-remove"></i> 删除
            </a>-->
            <a class="btn btn-warning" onclick="$.table.exportExcel()">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" data-mobile-responsive="true"></table>
        </div>
    </div>

    <!--弹框-->
    <div class="modal fade bs-example-modal-sm" id="selectRole" tabindex="-1" role="dialog" aria-labelledby="selectRoleLabel">
        <div class="modal-dialog modal-sm" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="selectRoleLabel">分配角色</h4>
                </div>
                <div class="modal-body">
                    <form id="boxRoleForm">
                        <div class="zTreeDemoBackground left">
                            <ul id="treeDemo" class="ztree"></ul>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
    <!--/弹框-->
</div>
<script th:inline="javascript">
    var editFlag = "[[${@perms.hasPerm('user:edit')}]]";
    var deleteFlag = "[[${@perms.hasPerm('user:delete')}]]";
    var saveUserRoles="[[${@perms.hasPerm('user:saveUserRoles')}]]";
    $(function() {
        var options = {
            url:ctx + "user/list",
            createUrl:ctx + "user/add",
            updateUrl: ctx + "user/edit/{id}",
            sortName: "id",
            modalName: "用户",
            search: false,
            showExport: false,
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '用户编号'
                },
                {
                    field: 'userId',
                    title: 'uuid'
                },
                {
                    field: 'username',
                    title: '用户名'
                },
                {
                    field: 'email',
                    title: '邮箱'
                },
                {
                    field: 'phone',
                    title: '电话'
                },
                {
                    field: 'sex',
                    title: '性别',
                    formatter: function(value, row, index) {
                        switch (row.sex){
                            case 1 : return '<span class="badge badge-info">男</span>';
                            case 2 : return '<span class="badge badge-info">女</span>';
                            case 3 : return '<span class="badge badge-info"><font style="color: red">未知</font></span>';
                        }
                    }
                },
                {
                    field: 'age',
                    title: '年龄'
                },
                {
                    field:'status',
                    title:'用户状态',
                    formatter: function(value, row, index) {
                        switch (row.status){
                            case 1 : return '<span class="badge badge-info">有效</span>';
                            case 2 : return '<span class="badge badge-info"><font style="color: red">无效</font></span>';
                        }
                    }
                },
                {
                    field: 'createtime',
                    title: '创建时间'
                },
                {
                    field: 'updatetime',
                    title: '修改时间'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push(editFlag=='true'?'<a class="btn btn-success btn-xs " href="#" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-check-square-o"></i>编辑</a> ':'');
                        actions.push(saveUserRoles=='true'?'<a class="btn btn-primary btn-xs " href="#"  onclick="rule('+row.id+')"><i class="fa fa-check-square-o"></i>分配角色</a> ':'');
                        actions.push(deleteFlag=='true'?'<a class="btn btn-danger btn-xs " href="#" onclick="deleteBy('+row.id+')"><i class="fa fa-remove"></i>删除</a>':'');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });
    /*用户管理-分配角色*/
    function rule(id) {
        console.log("分配角色");
        var $this = $(this);
        var userId = id;
        $.ajax({
            async: false,
            type: "POST",
            data: {userId: userId},
            url: '/user/rolesWithSelected',
            dataType: 'json',
            success: function (json) {
                var data = json.data;
                console.log(data);
                var setting = {
                    check: { enable: true, nocheckInherit: true, chkboxType: { "Y": "ps", "N": "ps" } },
                    data: {
                        simpleData: {
                            enable: true
                        }
                    },
                    callback: {
                        onCheck: function (event, treeId, treeNode) {
                            console.log(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);
                            var treeObj = $.fn.zTree.getZTreeObj(treeId);
                            var nodes = treeObj.getCheckedNodes(true);
                            var ids = new Array();
                            for (var i = 0; i < nodes.length; i++) {
                                //获取选中节点的值
                                ids.push(nodes[i].id);
                            }
                            //保存
                            $.post(ctx + "user/saveUserRoles", {"userId": userId, "roleIds": ids.join(",")});
                        }
                    }
                };
                var tree = $.fn.zTree.init($("#treeDemo"), setting, data);
                tree.expandAll(true);//全部展开

                $('#selectRole').modal('show');
            }
        });
    }
    /**删除**/
    function deleteBy(id) {
        $.modal.confirm("确定删除该条" + $.table._option.modalName + "信息吗？", function () {
            $.ajax({
                type: 'post',
                url: ctx + "user/delete/" + id,
                success : function(result) {
                    if (result.status == web_status.SUCCESS) {
                        $.modal.alertSuccess(result.message);
                        $.table.refresh();
                    } else {
                        $.modal.alertError(result.message);
                    }
                }
            });
        });
    }
</script>
</body>
</html>